package com.mycompany.view.web.application;

import javax.security.auth.login.FailedLoginException;

import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;
import org.apache.wicket.authorization.UnauthorizedInstantiationException;
import org.apache.wicket.authorization.strategies.page.AbstractPageAuthorizationStrategy;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;

import com.facebook.api.FacebookRestClient;
import com.mycompany.view.web.facebookIntegration.FaceBookAuthHandler;
import com.mycompany.view.web.facebookIntegration.FacebookSession;
import com.mycompany.view.web.pages.welcome.LoginPage;

/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see wicket.common.Start#main(String[])
 */
public class WicketFacebookIntegrationApplication extends WebApplication
					implements IUnauthorizedComponentInstantiationListener {    
	/** Logging */
	//private static final Log log = LogFactory.getLog(WicketFacebookIntegrationApplication.class);

	private static final String FACEBOOK_URL = "https://login.facebook.com/login.php?api_key=";
	
	private final String apiKey = "f7a94b1118f89c499fcb1f97b5c17a78";
	private final String apiSecretKey = "bc1bf6098b3e8d8ed69aad0834b6cbaa";
	
	public WicketFacebookIntegrationApplication() { }
	
	public Class getHomePage() {
		return LoginPage.class;
	}

    protected void init() {
        super.init();
        getSecuritySettings().setAuthorizationStrategy(new FaceBookAuthorizationStrategy());
        getSecuritySettings().setUnauthorizedComponentInstantiationListener(this);
    }

     public void onUnauthorizedInstantiation(Component component) {
        if (component instanceof Page) {
            Page page = (Page) component;
            FacebookSession pageSession = (FacebookSession) page.getSession(); 
            if (pageSession.getClient() != null) {
                return;
            }

            try {
                FacebookRestClient authClient = FaceBookAuthHandler.getAuthenticatedClient(page.getRequest(), apiKey, apiSecretKey);
                pageSession.setClient(authClient);
            } catch (FailedLoginException fle) {
                // "Session key not found"
            	//user not logged in
                forceLogin(page);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            throw new UnauthorizedInstantiationException(component.getClass());
        }
    }

    private void forceLogin(Page page) {
        page.getRequestCycle().setRequestTarget(new RedirectRequestTarget(FACEBOOK_URL + apiKey + "&v=1.0"));
    }

    @Override
    public Session newSession(Request request, Response response) {
    	return new FacebookSession(request);
    }
    
    private static class FaceBookAuthorizationStrategy extends AbstractPageAuthorizationStrategy {
        protected boolean isPageAuthorized(final Class pageClass) {
            return false;
        }
    }
}